// pages/account/booking/booking.js
let common = require('../../../utils/common.js');
Page({
  data: {
    tabActive: ['tab-active', '', ''],
    coaches: [],
    loadHidden: true,
    hasMore: false,
    tipNone: true,
    sltTypeid: 1,
  },
  skip: 0,
  take: 10,
  tabSwitch: function (e) {
    let that = this, idx = parseInt(e.currentTarget.dataset.index), tab = ['', '', ''];
    if (that.data.sltTypeid == (idx + 1)) return;
    tab[idx] = 'tab-active', that.skip = 0, that.take = 10;
    that.setData({
      tabActive: tab,
      coaches: [],
      sltTypeid: (idx + 1)
    });
    that.reqCoach();
  },
  reqCoach: function () {
    let that = this;
    common.asynPost(common.API.user.coachorders, {
      status: that.data.sltTypeid,
      skip: that.skip,
      take: that.take
    }, function (data) {
      that.setData({
        loadHidden: true,
      });
      if (data['orders'] && data['orders'].length) {
        that.setData({
          coaches: data['orders'],
          tipNone: true,
        });
      } else {
        that.skip -= that.take;
        that.setData({
          hasMore: true,
          tipNone: !that.data.coaches.length,
        });
      }
    }, function () {
      that.setData({
        loadHidden: true,
      });
    });
  },
  studying: function (e) {
    let that = this, id = parseInt(e.currentTarget.dataset.id);
    wx.showModal({
      title: '开始学习',
      content: '您确定要开始学习吗？',
      success(res) {
        if (res.confirm) {
          common.asynPost(common.API.user.learn, {
            id: id
          }, function (data) {
            wx.showToast({
              title: '已开始学习课程',
              icon: 'success'
            });
            that.setData({
              coaches: that.removeByValue(that.data.coaches, id)
            });
          }, function (data) {
            wx.showToast({
              title: '开始学习失败',
              icon: 'success'
            });
          });
        }
      }
    });
  },
  payOrder(e) {
    let that = this, id = parseInt(e.currentTarget.dataset.id);
    common.asynPost(common.API.user.payorder, { id: id }, function (data) {
      if (data['order']) {
        var result = data['order'];
        result.orderNo = result.code;
        result.orderId = result.id;
        wx.redirectTo({
          url: '../../pay/bookinginfo/bookinginfo?result=' + JSON.stringify(result)
        });
      }
    });
  },
  removeByValue(arr, val) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].id == val) {
        arr.splice(i, 1);
        break;
      }
    }
    return arr;
  },
  onReachBottom: function () {
    var that = this;
    that.setData({
      tipNone: false,
      hasMore: false
    });
    that.skip += that.take;
    that.reqCoach();
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      loadHidden: false
    });
    this.reqCoach();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})